// pages/home/index.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
var util = require('../../utils/util.js');
var ajax = require('../../utils/ajax.js');
var common = require('../../utils/common.js');
var PageBase = require('../../common/PageBase.js');
var app = getApp();

Page(common.extendPage(PageBase, {
  data: {
    loading: true,
    location: false,
    marqueePace: 1, //滚动速度
    marqueeDistance: 0, //初始滚动距离
    size: 14,
    orientation: 'left', //滚动方向
    interval: 15, // 时间间隔
    loaded: false,
    showAd: true,
    dataLoaded: false,
    mustLogin: true,
    noMore: true,
    autoplay: true,
    contactPhone:'',
    news: [],
    rczpList: [],
    bmfwList: [],
    tcxxList: [],
    hnycList: [],
    scsgList: [],
    tbqcList: [],
    bannerList: [],
    city: '',
    cateList: [{
        cateName: '加盟招商',
        imgUrl: '/imgs/home/home_jmzs.png',
        // url: '/pages/webview/index?dataUrl=https://www.shouchadou.com'
      },
      {
        cateName: '洗衣吧',
        imgUrl: '/imgs/home/home_xiyi.png',
        url: '/pages/shopList/index?type=1&title=洗衣吧'
      }, {
        cateName: '洗鞋吧',
        imgUrl: '/imgs/home/home_xixie.png',
        url: '/pages/shopList/index?type=2&title=洗鞋吧'
      },
      {
        cateName: '万能维修',
        imgUrl: '/imgs/home/home_wnwx.png',
        url: '/pages/shopList/index?type=7&title=万能维修'
      },
      {
        cateName: '客服电话',
        imgUrl: '/imgs/home/home_kf.png'
      }, {
        cateName: '法律咨询',
        imgUrl: '/imgs/home/home_flzx.png',
        url: '/pages/shopList/index?type=4&title=法律咨询'
      }, {
        cateName: '法律顾问',
        imgUrl: '/imgs/home/home_flgw.png',
        url: '/pages/shopList/index?type=4&title=法律顾问'
      }, {
        cateName: '诉讼业务',
        imgUrl: '/imgs/home/home_sxyw.png',
        url: '/pages/shopList/index?type=4&title=诉讼业务'
      }, {
        cateName: '申写文书',
        imgUrl: '/imgs/home/home_sxws.png',
        url: '/pages/shopList/index?type=4&title=申写文书'
      }, {
        cateName: '婚姻民事',
        imgUrl: '/imgs/home/home_hyms.png',
        url: '/pages/shopList/index?type=4&title=婚姻民事'
      }
    ]
  },
  /**
   * 初始化页面
   */
  onLoad() {
    qqmapsdk = new QQMapWX({
      key: 'XJCBZ-ER6CO-7QEWS-SAUJY-2JXH7-56BQ5'
    });
    this._loadingNum = 0;
    common.initApp({
      loginSuc: () => {
        util.getLoction(() => {
          var _this = this;
          qqmapsdk.reverseGeocoder({
            location: '', //获取表单传入的位置坐标,不填默认当前位置,示例为string格式
            success: function(res) { //成功后的回调
              var city = res.result.ad_info.city;
              wx.setStorageSync('locCity', city);
              if (!_this.data.city) {
                _this.setData({
                  city
                })
              }
            },
            fail: function(error) {
              console.error(error);
            },
            complete: function(res) {
              console.log(res);
            }
          })
          this.setData({
            location: wx.getStorageSync('location')
          })
        });
        this.getModule('rczp', (list) => {
          this.setData({
            rczpList: list
          })
        });
        this.getModule('bmfw', (list) => {
          this.setData({
            bmfwList: list
          })
        });
        this.getModule('tcxx', (list) => {
          this.setData({
            tcxxList: list
          })
        });
        this.getModule('hnyc', (list) => {
          this.setData({
            hnycList: list
          })
        });
        this.getModule('scsg', (list) => {
          this.setData({
            scsgList: list
          })
        });
        this.getModule('tbqc', (list) => {
          this.setData({
            tbqcList: list
          })
        });
        this.getModule('banner', (list) => {
          this.setData({
            bannerList: list
          })
        });
        this.getContact();
        this.getNewsList();
      }
    });
  },
  /**
   * 页面重载
   */
  onShow() {
    if (!!wx.getStorageSync('uid')) {
      this._mustLogin = false;
    } else {
      this._mustLogin = true;
    }
    var city = wx.getStorageSync('city') || wx.getStorageSync('locCity');
    this.setData({
      mustLogin: this._mustLogin,
      city
    })
  },
  /**
   * 分享
   */
  onShareAppMessage: function() {
    return this.$onShareAppMessage();
  },
  /**
   * 获取地理位置
   */
  _handler: function(res) {
    var _this = this;
    if (res.detail.authSetting['scope.userLocation']) {
      util.getLoction(() => {
        qqmapsdk.reverseGeocoder({
          location: '', //获取表单传入的位置坐标,不填默认当前位置,示例为string格式
          success: function(res) { //成功后的回调
            var city = res.result.ad_info.city;
            wx.setStorageSync('city', city);
            _this.setData({
              city
            })
          },
          fail: function(error) {
            console.error(error);
          },
          complete: function(res) {
            console.log(res);
          }
        })
        wx.setStorageSync('location', true)
        this.setData({
          location: true
        })
      });

    }
  },
  /**
   * 页面跳转
   */
  _jumpM(evt) {
    const {
      url
    } = evt.currentTarget.dataset;
    const dataUrl = url ? url.split('?dataUrl=')[1] : '';
    if (url) {
      if (this.data.mustLogin) {
        wx.navigateTo({
          url: '/pages/login/index',
        })
      } else {
        if (url.split('?dataUrl=')[0] == '/pages/webview/index' && !dataUrl) {
          return false;
        }
        this._jump(evt);
      }
    }
  },
  _call(evt){
    const phone = evt.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  /**
   * 获取首页数据
   */
  getModule(paramsId, cb) {
    var paramsUrl = '/public/api/index/' + paramsId;
    ajax.P({
      url: paramsUrl,
      data: {},
      success: (res, isLoadcb) => {
        var list = res.data;
        cb && cb(list);
        this._loadingNum--;
        if (this._loadingNum <= 0) {
          this.setData({
            loading: false
          })
        }
        isLoadcb && isLoadcb();
      },
      fail: (res) => {}
    })
  },
  /**
   * 获取跑马灯信息
   */
  getNewsList() {
    var paramsUrl = '/public/api/index/newslist';
    var paramsData = {
      page: 1,
      num: 1
    }
    ajax.P({
      url: paramsUrl,
      data: paramsData,
      success: (res, isLoadcb) => {
        this.setData({
          news: res.data[0].content
        })
        this._loadingNum--;
        if (this._loadingNum <= 0) {
          this.setData({
            loading: false
          })
        }
        isLoadcb && isLoadcb();
      },
      fail: (res) => {}
    })
  },
  getContact(){
    var paramsUrl = '/public/api/common/contact';
    ajax.P({
      url:paramsUrl,
      data:{},
      success:(res,isLoadcb)=>{
        this.setData({
          contactPhone:res.mobile
        })
        isLoadcb && isLoadcb();
      },
      fail: (res) => { }
    })
  }
}));